package com.treasure.web.controller.test;// Copyright (c) Alibaba, Inc. and its affiliates.

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import io.reactivex.Flowable;

import java.util.Collections;


public class QwenClient {

    public static final String MODEL_NAME = "qwen-turbo";

    public static void streamCallWithMessage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭，给我个菜谱").build();
        QwenParam param =
                QwenParam.builder().model(MODEL_NAME)
                        .messages(Collections.singletonList(userMsg))
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        Flowable<GenerationResult> result = gen.streamCall(param);
        result.blockingForEach(System.out::println);
    }

    public static void streamCallWithMessageCallback(String question, ResultCallback<GenerationResult> resultResultCallback)
            throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();
        QwenParam param =
                QwenParam.builder().model(MODEL_NAME)
                        .messages(Collections.singletonList(userMsg))
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        gen.streamCall(param, resultResultCallback);
    }


    public static void main(String[] args) {
//        Constants.apiKey = "sk-058db5a0e8b0446e90d6ae6b91222902";
        Constants.apiKey = "sk-9fa383c160e245d68ae95ad4d0276892";
        try {
            streamCallWithMessage();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}